package com.tb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tb.mapper.QuestionMapper;
import com.tb.model.*;
import com.tb.util.MyConstant;
import com.tb.util.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionService {

    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private QCommentService qCommentService;
    @Autowired
    private StarService starService;
    @Autowired
    private SensitiveWordService sensitiveWordService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private IllegalityService illegalityService;
    @Autowired
    private AnswerService answerService;

    public Question getQuestionWithCommentsById(Integer questionId) {
        Question question = getQuestionById(questionId);
        question.setqCommentList(qCommentService.getCommentByQuestionId(questionId));
        return question;
    }

    public Question getQuestionById(Integer questionId) {
        Question question = questionMapper.selectByPrimaryKey(questionId);
        question.setUser(userService.getUserById(question.getUserId()));
        question.setTopic(topicService.getTopicById(question.getTopicId()));
        return question;
    }

    public void deleteQuestionById(int id) {
        //删除之前连带删除消息表和违规记录表中的记录
        messageService.removeRecordByTypeAndObjectId(MyConstant.ACT_ASK,id);
        illegalityService.removeRecordByTypeAndObjectId(MyConstant.ACT_ASK,id);
        //删除旗下所有评论、答案
        List<QComment> qCommentList = qCommentService.getCommentByQuestionId(id);
        for (QComment qComment : qCommentList) {
            qCommentService.deleteQuestionCommentById(qComment.getId());
        }
        List<Answer> answerList = answerService.getAnswerByQuestionId(id);
        for (Answer answer : answerList) {
            answerService.deleteAnswerByAnswerId(answer.getId());
        }

        //真正删除问题记录
        questionMapper.deleteByPrimaryKey(id);
    }

    public int addQuestion(Question question) {
        String title = question.getTitle();
        question.setTitle(MyUtils.purifyString(title,sensitiveWordService.getStringList()));
        String content = question.getContent();
        question.setContent(MyUtils.purifyString(content,sensitiveWordService.getStringList()));
        questionMapper.insertSelective(question);
        return question.getId();
    }


    public PageInfo<Question> getQuestionByTopicIdAndPageID(Integer topicId, Integer pageId) {
//        PageBean<Question> pageBean = new PageBean<>();
//        pageBean.setCurPage(pageId);

        QuestionExample example = new QuestionExample();
        QuestionExample.Criteria criteria = example.createCriteria();
        criteria.andTopicIdEqualTo(topicId);

//        int pageCount = questionMapper.countByExample(example) / MyConstant.PAGE_SIZE_QUESTION + 1;
//        pageBean.setAllPage(pageCount);

        PageHelper.startPage(pageId, MyConstant.PAGE_SIZE_QUESTION);
//        pageBean.setList(questionMapper.selectByExample(example));
        List<Question> questionList = questionMapper.selectByExample(example);

        return new PageInfo<>(questionList);
    }

    public PageInfo<Question> getHotQuestionByPageID(Integer pageId) {
//        PageBean<Question> pageBean = new PageBean<>();
//        pageBean.setCurPage(pageId);

//        int pageCount = questionMapper.countByExample(null) / MyConstant.PAGE_SIZE_QUESTION + 1;
//        pageBean.setAllPage(pageCount);

        PageHelper.startPage(pageId, MyConstant.PAGE_SIZE_QUESTION);
//        pageBean.setList(questionMapper.selectQuestionOrderByAnswerCount());
        List<Question> questionList = questionMapper.selectQuestionOrderByAnswerCount();

        return new PageInfo<>(questionList);
    }

    public PageInfo<Question> getLatelyQuestionByPageID(Integer pageId) {
//        PageBean<Question> pageBean = new PageBean<>();
//        pageBean.setCurPage(pageId);

        QuestionExample example = new QuestionExample();
        example.setOrderByClause("time desc");

//        int pageCount = questionMapper.countByExample(example) / MyConstant.PAGE_SIZE_QUESTION + 1;
//        pageBean.setAllPage(pageCount);

        PageHelper.startPage(pageId, MyConstant.PAGE_SIZE_QUESTION);
//        pageBean.setList(questionMapper.selectByExample(example));
        List<Question> questionList = questionMapper.selectByExample(example);

        return new PageInfo<>(questionList);
    }

    public PageInfo<Question> getMyStarQuestionByPageID(Integer pageId, HttpServletRequest request) {
        int myUserId = Integer.parseInt(MyUtils.getCookie(request, "id").getValue());
        List<Star> starList = starService.getStarListByUserId(myUserId);
        //获取其中questionIdList
        List<Integer> questionIdList = new ArrayList<>();
        for (Star star : starList) {
            questionIdList.add(star.getQuestionId());
        }
        //根据questionIdList获取question
        List<Question> questionList = new ArrayList<>();
//        int pageCount = 0;
        if (!questionIdList.isEmpty()) {
            QuestionExample example = new QuestionExample();
            QuestionExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(questionIdList);
//            pageCount = questionMapper.countByExample(example) / MyConstant.PAGE_SIZE_QUESTION + 1;
            PageHelper.startPage(pageId, MyConstant.PAGE_SIZE_QUESTION);
            questionList = questionMapper.selectByExample(example);
        }
        //pageBean封装
//        PageBean<Question> pageBean = new PageBean<>();
//        pageBean.setCurPage(pageId);
//        pageBean.setAllPage(pageCount);
//        pageBean.setList(questionList);

        return new PageInfo<>(questionList);
    }

    public PageInfo<Question> getMyQuestionByPageID(Integer pageId, HttpServletRequest request) {
        int myUserId = Integer.parseInt(MyUtils.getCookie(request, "id").getValue());

        QuestionExample example = new QuestionExample();
        QuestionExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(myUserId);
        PageHelper.startPage(pageId, MyConstant.PAGE_SIZE_QUESTION);
        List<Question> questionList = questionMapper.selectByExample(example);
        //pageBean封装
//        PageBean<Question> pageBean = new PageBean<>();
//        pageBean.setCurPage(pageId);
//        int pageCount = questionMapper.countByExample(example) / MyConstant.PAGE_SIZE_QUESTION + 1;
//        pageBean.setAllPage(pageCount);
//        pageBean.setList(questionList);

        return new PageInfo<>(questionList);
    }

    public PageInfo<Question> getStarQuestionByKeyWordAndPageID(String keyWord, Integer pageId) {
        QuestionExample example = new QuestionExample();
        example.setOrderByClause("time desc");
        QuestionExample.Criteria criteria = example.createCriteria();
        QuestionExample.Criteria or = example.or();
        criteria.andTitleLike("%" + keyWord + "%");
        or.andContentLike("%" + keyWord + "%");
        PageHelper.startPage(pageId, MyConstant.PAGE_SIZE_QUESTION);
        List<Question> questionList = questionMapper.selectByExample(example);
        //pageBean封装
//        PageBean<Question> pageBean = new PageBean<>();
//        pageBean.setCurPage(pageId);
//        int pageCount = questionMapper.countByExample(example) / MyConstant.PAGE_SIZE_QUESTION + 1;
//        pageBean.setAllPage(pageCount);
//        pageBean.setList(questionList);

        return new PageInfo<>(questionList);
    }
}
